package com.echo.blog.business.blog.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.echo.blog.business.blog.model.BlogComment;
import com.echo.blog.business.blog.model.BlogReply;
import com.echo.blog.business.blog.service.IBlogCommentService;
import com.echo.blog.business.blog.service.IBlogReplyService;
import com.echo.blog.business.sys.model.SysUser;
import com.echo.model.common.RequestHolder;
import com.echo.model.constant.ConstantUtils;
import com.echo.model.constant.response.ResponseData;
import com.echo.model.context.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chentl
 * @version V1.0
 * @Project CommentController
 * @Title CommentController.java
 * @Description 评论 回复 控制器
 * @Package com.echo.blog.business.blog.controller
 * @date 2019/10/8 15:22
 * @Copyright: 上海顺益信息科技有限公司 All rights reserved.
 */
@RestController
@Slf4j
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private IBlogCommentService commentService;

    @Autowired
    private IBlogReplyService replyService;

    /**
     * @return com.echat.core.constant.response.ResponseData
     * @author chentl
     * @description 根据博客 id 获取评论信息
     * @date 2019/10/8 15:47
     * @params []
     * @since JDK 1.7
     */
    @RequestMapping(value = "/getCommentByBlogId")
    public ResponseData getTypeList(@Param("blogId") Integer blogId, @RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        Page<BlogComment> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        List<BlogComment> commentList = commentService.getCommentListByBlogId(blogId, page);
        page.setRecords(commentList);
        return new ResponseData(true, 200, "获取评论列表成功", page);
    }

    @RequestMapping(value = "/doComment")
    public ResponseData doComment(BlogComment blogComment) {
        LoginUser loginUser = RequestHolder.getCurrentUser();
        Integer loginId = loginUser.getId();
        //插入一条评论
        blogComment.setReviewerId(loginId);
        commentService.save(blogComment);
        return new ResponseData(true, 200, "评论成功", null);
    }

    @RequestMapping(value = "/likeOrNot")
    public ResponseData likeOrNot(@RequestParam("commentId") Integer commentId, @RequestParam("like") Integer like) {
        BlogComment commentDb = commentService.getById(commentId);
        Integer likeNum = commentDb.getLikeNum();
        if (like == ConstantUtils.Public.ONE) {
            //点赞
            likeNum++;
        }
        if (like == ConstantUtils.Public.ZERO) {
            //取消点赞
            likeNum--;
        }
        commentDb.setLikeNum(likeNum);
        commentService.updateById(commentDb);
        return new ResponseData(true, 200, "操作成功", null);
    }


    @RequestMapping(value = "/reply")
    public ResponseData reply(BlogReply reply) {
        LoginUser loginUser = RequestHolder.getCurrentUser();
        Integer loginId = loginUser.getId();
        reply.setReviewerId(loginId);
        replyService.save(reply);
        return new ResponseData(true, 200, "回复成功", null);
    }


}
